package masterlink.futures.ws;

import javax.xml.namespace.QName;
import org.apache.axis.client.*;
import org.slf4j.Logger;

import baytony.util.log.Log;

import javax.xml.rpc.ServiceException;
import java.net.MalformedURLException;

/**
 * http://192.168.6.70/AuthServices/Authentication_java.asmx?WSDL
 *
 * @author : tonyyang
 * Create date : 2011/11/21
 * @version 1.0.0	
 */
public class WSLogin implements Login {

	private static final Logger log=Log.getLogger(WSLogin.class);
	private final String endpoint;
	private Service service = new Service();

	public WSLogin(String endPoint) {
		this.endpoint = endPoint;
	}

	private Call createWebService() throws ServiceException,
			MalformedURLException {
		log.trace("Creating call to {}",endpoint);
		Call call = (Call) service.createCall();
		call.setTargetEndpointAddress(new java.net.URL(endpoint));
		return call;
	}

	/* (non-Javadoc)
	 * @see masterlink.futures.ws.Login#isOk(java.lang.String, java.lang.String)
	 */
	@Override
	public String[] checkToken(String token, String hostName) throws Exception {
		Call call = createWebService();
		log.trace("Calling to service with Hostname: {} and token : {}",hostName,token);

		call.setOperationName(new QName("http://masterlink.com.tw/Request",
				"AuthToken"));

		call.addParameter("Token", org.apache.axis.encoding.XMLType.XSD_STRING,
				javax.xml.rpc.ParameterMode.IN);

		call.addParameter("HostName",
				org.apache.axis.encoding.XMLType.XSD_STRING,
				javax.xml.rpc.ParameterMode.IN);

		call.setReturnType(org.apache.axis.encoding.XMLType.XSD_ANY);

		call.setUseSOAPAction(true);

		call.setSOAPActionURI("http://masterlink.com.tw/AuthToken");

		return (String[]) call.invoke(new Object[] { token,hostName });

	}
}
